package com.kmproject.haifu.mvp.presenter;

import android.app.Application;

import com.google.gson.Gson;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.FragmentScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import io.reactivex.Observable;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;

import com.jess.arms.utils.DataHelper;
import com.kmproject.haifu.app.AppConstant;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.app.utils.RxUtils;
import com.kmproject.haifu.mvp.contract.MyCollectHouseRentContract;
import com.kmproject.haifu.mvp.model.api.Api;
import com.kmproject.haifu.mvp.model.entity.ResultBean;
import com.kmproject.haifu.mvp.model.entity.beans.HouseRentListShowBean;
import com.kmproject.haifu.mvp.model.entity.beans.HouseRentListShowInfoBean;
import com.kmproject.haifu.mvp.model.entity.beans.RentHouseViewHistoryBean;
import com.kmproject.haifu.mvp.model.entity.beans.UserBean;
import com.kmproject.haifu.mvp.model.entity.beans.UserLoginBean;
import com.kmproject.haifu.mvp.model.entity.resbean.HouseParmarBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 07/11/2019 17:45
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@FragmentScope
public class MyCollectHouseRentPresenter extends BasePresenter<MyCollectHouseRentContract.Model, MyCollectHouseRentContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public MyCollectHouseRentPresenter(MyCollectHouseRentContract.Model model, MyCollectHouseRentContract.View rootView) {
        super(model, rootView);
    }

    /**
     * 查询搜藏列表
     * @param houseParmarBean
     */

    public void listHouseRentCurrentUser(HouseParmarBean houseParmarBean)
    {
        mModel.listHouseRentCurrentUser(AppUtil.entityToMap(houseParmarBean))
                .compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<List<HouseRentListShowInfoBean>>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<List<HouseRentListShowInfoBean>> listResultBean) {
                        if (AppUtil.isSuccess(listResultBean))
                        {
                            mRootView.getMeListSuccess(listResultBean.getData());
                        }else {
                            mRootView.showMessage(listResultBean.getMessage());
                        }
                    }
                });
    }
    /**
     * 过滤选中的内容的id
     *
     * @param houseRentListShowInfoBeans
     * @return
     */
    public List<String> getSelectIds(List<HouseRentListShowInfoBean> houseRentListShowInfoBeans) {
        List<String> ids = new ArrayList<>();
        if (houseRentListShowInfoBeans == null || houseRentListShowInfoBeans.isEmpty()) {
            return ids;
        }
        for (HouseRentListShowInfoBean houseRentListShowInfoBean : houseRentListShowInfoBeans) {
            if (houseRentListShowInfoBean.isHaveSelect()) {
                ids.add(houseRentListShowInfoBean.getRentId());
            }
        }
        return ids;
    }

    /**
     * 获取列表 老的
     *
     * @param page
     */
    public void getMeList(int type, String page) {
        if (type==3)
        {
            //获取浏览记录
            viewRecord(page);
            return;
        }
        //获取我的发布和收藏
        queryCurrentUserRentHouseList(type, page);
//        //服务器参数 0发布  1收藏 2浏览记录
//        String temUrl="";
//        if (type==3)
//        {
//            //浏览记录
//            temUrl="/app/listHouseRent/viewRecord/current";
//        }else  if (type==2){
//            //收藏
//            temUrl="/app/listHouseRent/collection/current";
//        }
//        else {
//            //发布
//            temUrl="/app/getHouseRentByUser";
//        }
//        UserBean userBean = DataHelper.getDeviceData(mAppManager.getCurrentActivity(), AppConstant.USER_INFO);//判断用户是否登陆
//
//        mModel.getMeList(Api.APP_DOMAIN+temUrl,String.valueOf(AppConstant.pageSize),String.valueOf(page),userBean.getUserId()).compose(RxUtils.applySchedulers(mRootView))
//             .subscribe(new ErrorHandleSubscriber<ResultBean<List<HouseRentListShowInfoBean>>>(mErrorHandler) {
//                 @Override
//                 public void onNext(ResultBean<List<HouseRentListShowInfoBean>> listResultBean) {
//                     if (AppUtil.isSuccess(listResultBean))
//                     {
//                         mRootView.getMeListSuccess(listResultBean.getData());
//                     }else {
//                         mRootView.showMessage(listResultBean.getMessage());
//                     }
//                 }
//             });

    }

    /**
     * 获取我的发布、收藏列表
     *
     * @param type
     * @param page
     */
    public void queryCurrentUserRentHouseList(int type, String page) {
        Map<String, Object> maps = new HashMap<>();
        //服务器参数 0发布  1收藏 2浏览记录
        if (type == 3) {
            //浏览记录
            maps.put("type", "2");
            //调用浏览器记录的方法，浏览记录是单独的
        } else if (type == 2) {
            //收藏
            maps.put("type", "1");
        } else {
            //发布
            maps.put("type", "0");
        }
        maps.put("page", page);
        maps.put("limit", String.valueOf(AppConstant.pageSize));
        mModel.queryCurrentUserRentHouseList(maps).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<List<HouseRentListShowInfoBean>>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<List<HouseRentListShowInfoBean>> listResultBean) {
                        if (AppUtil.isSuccess(listResultBean)) {
                            mRootView.getMeListSuccess(listResultBean.getData());
                        } else {
                            mRootView.showMessage(listResultBean.getMessage());
                        }
                    }
                });
    }

    /**
     * 删除我发布得租房
     *
     * @param temIds
     */
    public void del(int type, List<String> temIds) {
        removeCurrentUserRentHouseList(type,temIds);

//        if (type != 1) {
//            mRootView.showMessage("接口完善中");
//            return;
//        }
////        List<String> temIds=new ArrayList<>();
////        temIds.add(id);
//        mModel.delRentHouse(Api.APP_DOMAIN + "/app/removeHouseRent/", new Gson().toJson(temIds).toString()).compose(RxUtils.applySchedulers(mRootView))
//                .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
//                    @Override
//                    public void onNext(ResultBean resultBean) {
//                        if (AppUtil.isSuccess(resultBean)) {
//                            mRootView.delSuccess();
//                        } else {
//                            mRootView.showMessage(resultBean.getMessage());
//                        }
//                    }
//                });
    }

    /**
     * 删除选中的信息
     * @param type
     * @param temIds
     */
    public void removeCurrentUserRentHouseList(int type,List<String> temIds)
    {
        Map<String,Object> maps=new HashMap<>();
        maps.put("removeIds",temIds);
        //服务器参数 0发布  1收藏 2浏览记录
        if (type == 3) {
            //浏览记录
            maps.put("type", "2");
            //调用浏览器记录的方法，浏览记录是单独的
        } else if (type == 2) {
            //收藏
            maps.put("type", "1");
        } else {
            //发布
            maps.put("type", "0");
        }


        mModel.removeCurrentUserRentHouseList(maps).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean resultBean) {
                        mRootView.showMessage(resultBean.getMessage());
                      if (AppUtil.isSuccess(resultBean))
                      {
                      mRootView.delSuccess();
                      }
                    }
                });
    }

    /**
     * 查询浏览记录
     * @param
     * @param page
     */
   public void viewRecord( String page) {
     mModel.viewRecord(String.valueOf(AppConstant.pageSize),page).compose(RxUtils.applySchedulers(mRootView))
             .subscribe(new ErrorHandleSubscriber<ResultBean<List<RentHouseViewHistoryBean>>>(mErrorHandler) {
                 @Override
                 public void onNext(ResultBean<List<RentHouseViewHistoryBean>> listResultBean) {
                     if (AppUtil.isSuccess(listResultBean))
                     {
                       mRootView.viewRecordSuccess(listResultBean.getData());
                     }else {
                         mRootView.showMessage(listResultBean.getMessage());
                     }
                 }
             });
   }


        @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}
